package com.yingxin.yxerp.module.permission.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.helper.ListHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yx.framework.user.dto.AppPermissionDto;
import com.yingxin.yxerp.module.permission.entity.AppPermission;
import com.yingxin.yxerp.module.permission.entity.AppRole;
import com.yingxin.yxerp.module.permission.mapper.AppPermissionMapper;
import com.yingxin.yxerp.module.permission.mapper.AppRoleMapper;
import com.yingxin.yxerp.module.permission.service.IAppPermissionService;
import com.yingxin.yxerp.module.permission.service.IAppRoleService;
import com.yingxin.yxerp.module.permission.vo.req.RoleCreateVo;
import com.yingxin.yxerp.module.permission.vo.req.RoleQueryVo;
import com.yingxin.yxerp.module.permission.vo.req.RoleUpdateVo;
import com.yingxin.yxerp.module.permission.vo.resp.RoleRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**

 * 角色 前端控制器
 * @author fanchao
 * @since 2021-05-13
 */

@ApiSort(30)
@RestController
@RequestMapping("/permission/app-android/role")
public class AppRoleController {

    @Autowired
    private IAppRoleService appRoleService;

    @Autowired
    @SuppressWarnings("all")
    private AppRoleMapper appRoleMapper;

    @Autowired
    private IAppPermissionService appPermissionService;

    @Autowired
    @SuppressWarnings("all")
    private AppPermissionMapper appPermissionMapper;

    @Autowired
    private IUserSupport userSupport;

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<RoleRespVo> list(@RequestBody RoleQueryVo queryVo) {
        return appRoleMapper.listVo(queryVo);
    }


    @ApiOperation(value = "查询可用角色列表（全部）")
    @PostMapping("/list/enable")
    public List<RoleRespVo> listEnable(@RequestBody RoleQueryVo queryVo) {
        queryVo.setStatus(CommonConst.TRUE);
        return appRoleMapper.listVo(queryVo);
    }


    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public RoleRespVo get(@PathVariable("id") String id) {
        RoleRespVo role = appRoleMapper.getVo(id);
        List<AppPermissionDto> rolePermissions = appPermissionMapper.listDtoByRoleId(id);
        Map<String, AppPermissionDto> rolePermissionsMap = ListHelper.toMap(AppPermissionDto::getId, rolePermissions);
        List<AppPermission> allPermissions = appPermissionService.list();
        for (AppPermission permission : allPermissions) {
            rolePermissionsMap.remove(permission.getParentId());
        }
        rolePermissions = new ArrayList<>(rolePermissionsMap.values());
        role.setAppPermissions(rolePermissions);
        role.setPermissionIds(ListHelper.toList(AppPermissionDto::getId, rolePermissions));
        return role;
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid RoleCreateVo createVo) {
        // 角色
        AppRole role = new AppRole();
        BeanUtil.copyProperties(createVo, role);
        appRoleService.create(role, createVo.getPermissionIds());
        return role.getId();
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid RoleUpdateVo updateVo) {
        // 角色
        AppRole role = new AppRole();
        BeanUtil.copyProperties(updateVo, role);
        appRoleService.update(role, updateVo.getPermissionIds());
        return role.getId();
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        AppRole role = appRoleService.getById(id);
        appRoleService.delete(role);
    }


    @ApiOperation("启用")
    @PostMapping("/enable/{id}")
    public void enable(@PathVariable("id") String id) {
        AppRole role = appRoleService.getById(id);
        appRoleService.enable(role);
    }

    @ApiOperation("禁用")
    @PostMapping("/disable/{id}")
    public void disable(@PathVariable("id") String id) {
        AppRole role = appRoleService.getById(id);
        appRoleService.disable(role);
    }
}
